﻿// SyntheticDataGenerator.SequenceCreator
//
// (c) 2011 Arthur Pitman
//
// Part of Market-Basket Synthetic Data Generator
// A C# adaptation of the IBM Quest Market-Basket Synthetic Data Generator
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// (Version 2) as published by the Free Software Foundation.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public
// License (Version 2) along with this program; if not, write to 
// the Free Software Foundation, Inc., 
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// or see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SyntheticDataGenerator.Generators;
using System.IO;

using SyntheticDataGenerator.ParameterSets;
using SyntheticDataGenerator.Core;

namespace SyntheticDataGenerator
{
	/// <summary>
	/// Creates sequences
	/// </summary>
	public class SequenceCreator
	{
		/// <summary>
		/// Initializes a new <see cref="SequenceCreator"/> instance
		/// </summary>
		/// <param name="parameters"></param>
		public SequenceCreator(SequenceParameters parameters)
		{
			Console.WriteLine("Creating sequences:");
			parameters.Write(Console.Out);
			parameters.WriteToFile(parameters.BaseFilename + ".config");

			// set master seed for random seed generator
			if (parameters.MasterSeed < 0)
				SeedGenerator.SetMasterSeed(parameters.MasterSeed);

			// large sequences patterns
			PatternSet lseqPatternSet = new PatternSet(parameters.LisParameters.PatternCount, parameters.LSeqParameters);
			var seqPatternWriter = File.CreateText(parameters.BaseFilename + ".seq.patterns");
			lseqPatternSet.Write(seqPatternWriter);
			seqPatternWriter.Close();

			// large itemsets patterns
			PatternSet litsPatternSet = new PatternSet(parameters.ItemCount, parameters.LisParameters);
			var litPatternWriter = File.CreateText(parameters.BaseFilename + ".lit.patterns");
			litsPatternSet.Write(litPatternWriter);
			litPatternWriter.Close();

			var patternSetIterator = new PatternSetIterator(lseqPatternSet);
			var sequenceLengthGenerator = new PoissonGenerator(parameters.AverageSequenceLength - 1);
			var transactionLengthGenerator = new PoissonGenerator(parameters.AverageTransactionLength - 1);
				   
			// create and write sequences to file
			var sequenceWriter = File.CreateText(parameters.BaseFilename + ".sequences");
			for (int i = 0; i < parameters.SequenceCount; i++)
			{
				Sequence sequence = new Sequence(i + 1, patternSetIterator, litsPatternSet, sequenceLengthGenerator.NextInt() + 1, transactionLengthGenerator.NextInt() + 1);
				if (parameters.FlatSequences)
					sequence.WriteFlat(sequenceWriter);
				else
					sequence.Write(sequenceWriter);
			}
			sequenceWriter.Close();
		}
	  
	}
}